package fr.utbm.tx52.stamp.logic.utils;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import fr.utbm.tx52.stamp.logic.Constant;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Classe permettant la manipulation des fichiers
 *
 * @author Julien GONZALVES (julien.gonzalves@utbm.fr) Laurent RUBY
 * (laurent.ruby@utbm.fr)
 */
public class FileUtil
{

	public static File checkOrCreatePicturesFolder()
	{
		File folderName = new File(Constant.PICTURES_PATH_FOLDER);
		if (!folderName.exists())
		{
			folderName.mkdir();
		}
		return folderName;
	}

	/**
	 * Lit le contenu de la carte SD
	 *
	 * @return une ArrayList des fichiers se trouvant sur la carte SD
	 */
	public static ArrayList<File> getSDCardContent()
	{
		ArrayList<File> alFiles = new ArrayList<File>();
		File folderName = checkOrCreatePicturesFolder();
		File[] files = folderName.listFiles();
		if (files != null && files.length > 0)
		{
			for (File currentFile : files)
			{
				//Si l'�l�ment courant est une fichier qui n'est pas de type 'son'
				//Alors on l'ajoute � la liste des �l�ments � prendre en compte dans la galerie
				if (!currentFile.getName().contains(Constant.FILE_SOUND_EXTENTION) && currentFile.isFile())
				{
					alFiles.add(currentFile);
				}
			}
		}
		return alFiles;
	}

	public static Bitmap loadBitmap(ContentResolver contentResolver, Uri uri)
	{
		Bitmap bm = null;
		InputStream is = null;
		BufferedInputStream bis = null;
		int size = 0;
		try
		{
			is = contentResolver.openInputStream(uri);
			bis = new BufferedInputStream(is, 8192);
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
			bm = BitmapFactory.decodeStream(bis, null, options);
			size = (options.outWidth + options.outHeight) / 2;
		}
		catch (Exception e)
		{
			Log.e(FileUtil.class.getSimpleName(), "", e);
		}
		finally
		{
			if (bis != null)
			{
				try
				{
					bis.close();
				}
				catch (IOException e)
				{
					Log.e(FileUtil.class.getSimpleName(), "", e);
				}
			}
			if (is != null)
			{
				try
				{
					is.close();
				}
				catch (IOException e)
				{
					Log.e(FileUtil.class.getSimpleName(), "", e);
				}
			}
		}

		try
		{
			is = contentResolver.openInputStream(uri);
			bis = new BufferedInputStream(is, 8192);
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = (size > 560) ? 2 : 1;
			bm = BitmapFactory.decodeStream(bis, null, options);
		}
		catch (Exception e)
		{
			Log.e(FileUtil.class.getSimpleName(), "", e);
		}
		finally
		{
			if (bis != null)
			{
				try
				{
					bis.close();
				}
				catch (IOException e)
				{
					Log.e(FileUtil.class.getSimpleName(), "", e);
				}
			}
			if (is != null)
			{
				try
				{
					is.close();
				}
				catch (IOException e)
				{
					Log.e(FileUtil.class.getSimpleName(), "", e);
				}
			}
		}
		return bm;
	}

	private static String changeExtension(String path, String toExt)
	{
		String result = null;
		String[] splitted = path.split("\\.");
		if (splitted.length != 0)
		{
			splitted[(splitted.length - 1)] = toExt;
			result = "";
			for (String split : splitted)
			{
				result += split + ".";
			}
			result = result.substring(0, result.length() - 1);
		}
		return result;
	}

	public static String getSoundPath(String imagePath)
	{
		return changeExtension(imagePath, Constant.FILE_SOUND_EXTENTION);
	}

	public static String getCommentPath(String imagePath)
	{
		return changeExtension(imagePath, Constant.FILE_TEXT_EXTENSION);
	}

	public static String getExtension(String path)
	{
		String[] splitted = path.split("\\.");
		if (splitted.length != 0)
		{
			return splitted[(splitted.length - 1)];
		}
		return "";
	}
}
